/* This class demonstrates how to assign this object to another objects member
 * variable which is a reference.
 * http://cplus.about.com/od/learning1/ss/constructors_4.htm
 *
 */

#include <iostream>

using namespace std;

class A;

class B
{
  public:
    B(A &a) : m_ref_a(a)
    {
    }
    /*
    void print_A_integar() //if define the function here, it will have error: "this_to_object_reference.cpp:22: error: invalid use of incomplete type ‘struct A’"
    {
      cout << "ref_a.integar = " << m_ref_a.getInt() << endl;
    }
    */
    void print_A_integar();//need to define this function after define class A.

  private:
    A &m_ref_a;
};

class A
{
  public:
    A() : m_integar(3), m_b(*this)
    {
    
    }
    B &getB()
    {
      return m_b;
    }

    void setInt(int integar)
    {
      this->m_integar = integar;
    }

    int getInt()
    {
      return this->m_integar;
    }

  private:
    B m_b;
    int m_integar;
};

void B::print_A_integar()
{
  cout << "ref_a.integar = " << m_ref_a.getInt() << endl;
}

/*
class B
{
  public:
    B(A &a)
    {
      this->m_ref_a = a;
    }

    print_A_integar()
    {
      cout << "ref_a.integar = " << ref_a.integar << endl;
    }
  private:
    A &m_ref_a;
};
*/

int main()
{
  A a;
  a.getB().print_A_integar();
  a.setInt(10);
  a.getB().print_A_integar();
  return 0;
}
